package com.karst.essentials.chapter04
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	
	import org.aswing.AsWingManager;
	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Cylinder;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
	
	internal class FinalMaterialExample extends Sprite
	{
		public function FinalMaterialExample()
		{
			super();
			createView() ;
		}

		/** 声明一个集成视图对象  */
		private var view : BasicView = null ; 
		/** 声明一个三维显示对象  */
		private var do3d : DisplayObject3D = null ;
		/** 并创建圆柱作为父对象（材质，半径，高，横向片面数，纵向片面数）  */
		private var parentObj : Cylinder = null ;
		private function createView() : void
		{
			if( view == null )
			{
				view = new BasicView() ;
				view.camera.z = 200 ;
				view.camera.zoom = 40 ;
				view .viewport.interactive = true ;
			}
			
			if( do3d == null )
				do3d = new DisplayObject3D() ;
			
			if( parentObj == null )
				parentObj = new Cylinder( null, 250, 180, 15, 2 ) ;
			
			//获取圆柱的顶点个数。“.geometry.vertices”就是一个数组。当然可以用（纵向片面数+1）*横向片面数 得到
			var num : uint = parentObj.geometry.vertices.length ;
			for( var i : uint = 0; i < num; i++ )
			{
				//在每个顶点创建一个平面
				//声明并创建平面（材质，宽，高），调用方法（0）来随机创建材质
				var childObj : Plane = new Plane( getRandomMaterial(), 75, 75, 2, 2 ) ;
				//调整全部平面的姿势，使其能面向观众
				//Math.atan2(y,x)以弧度为单位计算并返回点 y/x 的角度值，
				//该角度从圆的 x 轴（其中，0,0 表示圆心）沿逆时针方向测量，atan2 的第一个参数始终是 y 坐标。
				//角度=弧度*180/Math.PI，注意查看<AS3语言和组件参考>。这里为负值是考虑到坐标方向，
				//PV3D中默认逆时针为正方向
				childObj.x = parentObj.geometry.vertices[i].x ;
				childObj.y = parentObj.geometry.vertices[i].y ;
				childObj.z = parentObj.geometry.vertices[i].z ;
				childObj.localRotationY = -Math.atan2( childObj.x, childObj.z ) * 180 / Math.PI ;
				childObj.addEventListener( InteractiveScene3DEvent.OBJECT_OVER, rollOverHandler ) ;
				do3d.addChild( childObj ) ;
			}
			
			addChild( view ) ;
			view.scene.addChild( do3d ) ;
			view.addEventListener( Event.ENTER_FRAME, enterHandler ) ;
		}
		
		private function getRandomMaterial() : MaterialObject3D
		{
			var random : int = Math.round( Math.random() * 2 ) ;
			if( random == 0 )
			{
				return createMovieMaterial() ;
			}
			else if( random == 1 )
			{
				return createBitmapFileMaterial() ;
			}
			else
			{
				return createColorMaterial() ;
			}
		}
		
		private function rollOverHandler( event : InteractiveScene3DEvent ) : void
		{
			if( event.displayObject3D.material is ColorMaterial )
			{
				event.displayObject3D.material= createColorMaterial() ;
			}
		}
		
		private function createMovieMaterial() : MovieMaterial
		{
			var imgLoader : Loader = new Loader() ;
			imgLoader.x = 100 ;
			imgLoader.y = 150 ;
			imgLoader.load( new URLRequest( "01.jpg" ) ) ;
			
			var flipSprite : Sprite = new Sprite() ;
			flipSprite.graphics.beginFill( 0x3366FF ) ;
			flipSprite.graphics.drawRect( 0,0, 512,512 ) ;
			flipSprite.graphics.endFill() ;
			
			flipSprite.addChild( imgLoader ) ;
			
			imgLoader.addEventListener( MouseEvent.CLICK, flipClickHandler ) ;
			imgLoader.addEventListener( MouseEvent.MOUSE_OVER, flipRollOverHandler ) ;
			imgLoader.addEventListener( MouseEvent.MOUSE_OUT, flipRollOutHandler ) ;
			
			var materialSprite : Sprite = new Sprite() ;
			materialSprite.addChild( flipSprite ) ;
			
			var material : MovieMaterial = new MovieMaterial( materialSprite, false, true ) ;
			material.interactive = true ;
			return material ;
		}
		
		private function createBitmapFileMaterial() : BitmapFileMaterial
		{
			var arr : Array = ["front", "back", "top", "bottom", "left", "right" ] ;
			var random : int = Math.round( Math.random() * 9 ) ;
			if( random < 7 )
			{
				var materail : BitmapFileMaterial = new BitmapFileMaterial( "01.jpg" ) ;
			}
			return materail ;
		}
		
		private function createColorMaterial() : ColorMaterial 
		{ 
			//方法（4）创建单色材质
			return new ColorMaterial(0xFFFFFF * Math.random(),1,true);
			//16进制RGB色（随机的），alpha值1为不透明，交互属性
		}
		
		
		
			
		private function flipClickHandler( event : MouseEvent ) : void
		{
			var targetFlip : DisplayObject = (event.target as Loader).content.parent ;
			if( targetFlip.scaleY == -1 )
			{
				targetFlip.scaleY = 1 ;
			}
			else
			{
				targetFlip.scaleY = -1 ;
			}
		}
		
		private function flipRollOverHandler(e:MouseEvent):void
		{ 
			view.viewport.buttonMode = true; //开启场景.窗口.按钮模式，鼠标变成小手
		}
		
		private function flipRollOutHandler(e:MouseEvent):void 
		{ 
			view.viewport.buttonMode = false; //关闭按钮模式
		}
		
		private function enterHandler(e:Event):void 
		{ 
			//方法（Final），最终渲染场景让我们看到
			do3d.localRotationY +=( AsWingManager.getStage().mouseX - AsWingManager.getStage().stageWidth / 2) / 200;
			view.singleRender();
		}
	}
}